home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Arashi 1.1 / Game Source / jam src / NewTitleEffect.c < prev    next >
C/C++ Source or Header  |  1993-07-23  |  6KB  |  280 lines

  1. /*/
  2.      Project Arashi: NewTitleEffect.c
  3.      Major release: Version 1.1, 7/22/92
  4.  
  5.      Last modification: Friday, July 23, 1993, 0:25
  6.      Created: Sunday, March 14, 1993, 22:00
  7.  
  8.      Copyright © 1993, Juri Munkki
  9. /*/
  10.  
  11. #include "VA.h"
  12. #include "ThingBlocks.h"
  13. #include "NewTitleEffect.h"
  14.  
  15. void    VAFractalLine(int,int,int,int,int,int);
  16.  
  17. typedef struct
  18. {
  19.     int        state;
  20.  
  21.     long    x,y;
  22.     int        sx,sy;
  23.     int        timer;
  24. }    LiveLineSegment;
  25.  
  26. enum    /*    States    */
  27. {    startState=0, sparkState, solidState, lastState    };
  28.  
  29. ThingBlock    PolyThing;
  30.  
  31. void    ScalePolyThingToRect(ThingBlock    theThing, Rect *newBounds)
  32. {
  33.     Rect                CurrentBounds;
  34.     int                    counter;
  35.     ThingBlockHeader    *thing;
  36.     LiveLineSegment        *lp;
  37.     char                state;
  38.     int                    x,y;
  39.     long                o1x,o1y,s1x,s1y;
  40.     long                o2x,o2y,s2x,s2y;
  41.     
  42.     state = HGetState(theThing);
  43.     HLock(theThing);
  44.     thing = *theThing;
  45.     
  46.     counter = thing->numItems;
  47.     lp = (void *)thing->theStuff;
  48.     SetRect(&CurrentBounds,32767,32767,-32768,-32768);
  49.     
  50.     while(counter--)
  51.     {    x = lp->x;
  52.         y = lp->y;
  53.         if(x > CurrentBounds.right)        CurrentBounds.right = x;
  54.         if(x < CurrentBounds.left)        CurrentBounds.left = x;
  55.         if(y > CurrentBounds.bottom)    CurrentBounds.bottom = y;
  56.         if(y < CurrentBounds.top)        CurrentBounds.top = y;
  57.         lp++;
  58.     }
  59.     
  60.     o1x = (CurrentBounds.left+CurrentBounds.right)/2;
  61.     o1y = (CurrentBounds.top+CurrentBounds.bottom)/2;
  62.     s1x = CurrentBounds.right - CurrentBounds.left;
  63.     s1y = CurrentBounds.bottom - CurrentBounds.top;
  64.     
  65.     o2x = (newBounds->left+newBounds->right)/2;
  66.     o2y = (newBounds->top+newBounds->bottom)/2;
  67.     s2x = newBounds->right - newBounds->left;
  68.     s2y = newBounds->bottom - newBounds->top;
  69.     
  70.     /*    Maintain aspect ratio.    */
  71.     if(s1x * s2y < s1y * s2x)
  72.     {    s2x = s2y;
  73.         s1x = s1y;
  74.     }
  75.  
  76.     counter = thing->numItems;
  77.     lp = (void *)thing->theStuff;
  78.     while(counter--)
  79.     {    lp->x = o2x + ((lp->x - o1x) * s2x) / s1x;
  80.         lp->y = o2y + ((lp->y - o1y) * s2x) / s1x;
  81.         lp->sx = lp->sx * s2x / s1x;
  82.         lp->sy = lp->sy * s2x / s1x;
  83.         lp++;
  84.     }
  85.  
  86.     HSetState(theThing, state);
  87. }
  88.  
  89. pascal    void    MyStdPoly(grafVerb,thePoly)
  90. int            grafVerb;
  91. PolyHandle    thePoly;
  92. {
  93.     long            polysize;
  94.     Point            *p;
  95.     LiveLineSegment    newsegment;
  96.     char            state;
  97.     
  98.     StdPoly(grafVerb,thePoly);
  99.  
  100.     state = HGetState(thePoly);
  101.     HLock(thePoly);
  102.     polysize = (GetHandleSize(thePoly)-14)>>2;
  103.     p = (*thePoly)->polyPoints;
  104.     
  105.     while(polysize-- > 0)
  106.     {    newsegment.x = p->h;
  107.         newsegment.y = p->v;
  108.         p++;
  109.         newsegment.sx = p->h - newsegment.x;
  110.         newsegment.sy = p->v - newsegment.y;
  111.         newsegment.state = startState;
  112.         AddThing(PolyThing,&newsegment);
  113.     }
  114.  
  115.     HSetState(thePoly,state);
  116. }
  117.  
  118. void    ConvertToPolys(resId)
  119. int        resId;
  120. {
  121.     long        someBits[10];
  122.     BitMap        someMap;
  123.     PicHandle    thePicture;
  124.     GrafPtr        tempPort;
  125.     GrafPtr        saved;
  126.     CQDProcs    myQDProcs;
  127.  
  128.  
  129.     PolyThing = NewThingBlock(sizeof(LiveLineSegment));
  130.  
  131.     GetPort(&saved);
  132.     
  133.     tempPort = (GrafPtr) NewPtr(sizeof(GrafPort));
  134.     OpenPort(tempPort);
  135.     SetPort(tempPort);
  136.  
  137.     someMap.baseAddr = (Ptr)someBits;
  138.     SetRect(&someMap.bounds,0,0,16,5);
  139.     someMap.rowBytes = 4;
  140.     SetPortBits(&someMap);
  141.  
  142.     SetStdCProcs(&myQDProcs);
  143.  
  144.     myQDProcs.polyProc = (Ptr)MyStdPoly;
  145.     tempPort->grafProcs = (void *)&myQDProcs;
  146.  
  147.     thePicture = GetPicture(resId);
  148.     HLock(thePicture);
  149.     DrawPicture(thePicture,&((*thePicture)->picFrame));
  150.     HUnlock(thePicture);
  151.     ReleaseResource(thePicture);
  152.  
  153.     SetPort(saved);
  154.  
  155.     tempPort->grafProcs = 0;
  156.     ClosePort(tempPort);
  157.     DisposPtr(tempPort);
  158.  
  159.     if(0)
  160.     {    Rect    testr = { 10, 10, 120, 400};
  161.         ThingBlockHeader    *thing;
  162.         LiveLineSegment        *lp;
  163.         int                    counter;
  164.     
  165.         ScalePolyThingToRect(PolyThing,&testr);
  166.         EraseRect(&testr);
  167.         FrameRect(&testr);
  168.         HLock(PolyThing);
  169.         thing = *PolyThing;
  170.         
  171.         counter = thing->numItems;
  172.         lp = (void *)thing->theStuff;
  173.  
  174.         while(counter--)
  175.         {    MoveTo(lp->x,lp->y);
  176.             Line(lp->sx,lp->sy);
  177.             lp++;
  178.         }
  179.         
  180.         while(!Button());
  181.     }
  182. }
  183.  
  184. void    titlemain(ResPictNum, fadeDirection)
  185. int        ResPictNum;
  186. int        fadeDirection;
  187. {
  188.     EventRecord            Event;
  189.     int                    i;
  190.     Rect                titleRect;
  191.     ThingBlockHeader    *thing;
  192.     LiveLineSegment        *lp;
  193.     int                    counter;
  194.     int                    sweep,dsweep;
  195.     int                    doneOnce = 0;
  196.         
  197.     ConvertToPolys(ResPictNum);
  198.     randSeed=TickCount();
  199.     
  200.     VA.FrameSpeed=3; 
  201.     
  202.     titleRect = VA.frame;
  203.     InsetRect(&titleRect,titleRect.right >> 3, titleRect.bottom >> 3);
  204.     ScalePolyThingToRect(PolyThing,&titleRect);
  205.     HLock(PolyThing);
  206.     thing = *PolyThing;
  207.  
  208.     sweep = 0;
  209.     if (fadeDirection == HORIZONTAL)
  210.         dsweep = 1 + (titleRect.right >> 6);
  211.     else
  212.     {    
  213.         dsweep = 1 + (titleRect.bottom >>7);
  214.         sweep += 2*dsweep;
  215.     }
  216.  
  217.     do
  218.     {    Boolean    needsDraw;
  219.     
  220.         needsDraw = FALSE;
  221.  
  222.         counter = thing->numItems;
  223.         lp = (void *)thing->theStuff;
  224.         
  225.         sweep += dsweep;
  226.         
  227.         while(counter--)
  228.         {    switch(lp->state)
  229.             {    case startState:
  230.                     if (fadeDirection == HORIZONTAL)
  231.                     {
  232.                         if(sweep > lp->x+(lp->sx >> 1))
  233.                         {    lp->state = sparkState;
  234.                             lp->timer = 6;
  235.                         }
  236.                     }
  237.                     else
  238.                         if(sweep > lp->y+(lp->sy >> 1))
  239.                         {    lp->state = sparkState;
  240.                             lp->timer = 6;
  241.                         }
  242.                     break;
  243.                 case sparkState:
  244.                     needsDraw = TRUE;
  245.                     VA.color = 1;
  246.                     VAFractalLine(lp->x,lp->y,
  247.                                   lp->x+lp->sx,lp->y+lp->sy,
  248.                                   1<<(8-(lp->timer >> 1)),3);
  249.                     if(lp->timer-- <= 0)
  250.                     {    lp->state = solidState;
  251.                         lp->timer = 100;
  252.                     }
  253.                     break;
  254.                 case solidState:
  255.                     needsDraw = TRUE;
  256.                     VA.color = 0;
  257.                     VALine(lp->x,lp->y,lp->x+lp->sx,lp->y+lp->sy);
  258.                     lp->timer--;
  259.                     if(lp->timer < 0)
  260.                     {    lp->state = startState;
  261.                         sweep = 0;
  262.                     }
  263.                     break;
  264.             }
  265.             lp++;
  266.         }
  267.         
  268.         if (sweep==0)
  269.         {    
  270.             doneOnce=1;
  271.         }
  272.         VA.segmscale=5;
  273.         VA.color=BG1;
  274.         
  275.         if(needsDraw)
  276.             VAStep();
  277.         else
  278.             VACatchUp();
  279.      } while( (!Button()) && (!(doneOnce)) && (!(GamePeekEvent())) ); 
  280. }